package com.t235.gc.device.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.device.service.DeviceService;
import com.t235.gc.devicevo.AddDeviceVo;
import com.t235.gc.devicevo.DeviceVo;
import com.t235.gc.entity.Device;
import com.t235.gc.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description :
 * Created by Li on 2021-03-23 09:20:14
 */
@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/api/device/admin")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "设备首页查询", httpMethod = "GET", response = AjaxResult.class, notes = "可以无参")
    @GetMapping("/getBackDeviceList")

    public AjaxResult findByAll(DeviceVo deviceVo ,@RequestParam(name = "page" ,defaultValue = "0") Integer pageNum,
                                @RequestParam(name = "limit",defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceVo> byAll = deviceService.findByAll(deviceVo);
        PageInfo<DeviceVo> pageInfo = new PageInfo<>(byAll);
        return AjaxResult.success(pageInfo);
    }
    @GetMapping("/getBackDeviceCountStatistics")
    public AjaxResult findIndexHeadNumber() {
        return AjaxResult.success(deviceService.findIndexHeadNumber());
    }

    //获取设备条件查询下拉框信息
    @GetMapping("/getDeviceCriteriaQuerySelect")
    public AjaxResult getDeviceCriteriaQuerySelect() {
        return AjaxResult.success(deviceService.getDeviceCriteriaQuerySelect());
    }

    @GetMapping("/getAddDeviceOfSelect")
    public AjaxResult getAddDeviceOfSelectAlter(String managerId) {
            return AjaxResult.success(deviceService.gitRatioByManagerId(managerId));
    }

    @GetMapping("/getVenuesByName")
    public AjaxResult getVenuesByName(String venuesName) {
        return AjaxResult.success(deviceService.gitAddressByVenuesName(venuesName));
    }

    @PostMapping("/addDevice")
    public AjaxResult addDevice (AddDeviceVo addDeviceVo){
        deviceService.addDevice(addDeviceVo);
        return AjaxResult.success("添加成功");
    }

    @PutMapping("/saveOrUpdateDevice")
    public AjaxResult saveOrUpdateDevice(AddDeviceVo addDeviceVo) {
        if (deviceService.updDevice(addDeviceVo)) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }


    //升级版本
    @PostMapping("/deviceVersionUpgrade")
    public AjaxResult deviceVersionUpgrade(Long deviceid, String name) {
        if (deviceService.updDeviceVersion(deviceid,name)) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    //删除设备
    @DeleteMapping("/delDevice")
    public AjaxResult delDevice(Long deviceId) {
        if (deviceService.delDevice(deviceId) > 0) {
            return AjaxResult.success("删除成功");
        }
       return AjaxResult.error("删除失败");
    }

    //导出表格
    @RequestMapping("/exportDevExcel")
    public AjaxResult exportExcel(HttpServletResponse response) throws Exception {
        try {
            ExcelUtils.export(deviceService.findByAll(new DeviceVo()),response);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    //根据deviceId查询设备信息
    @RequestMapping(value = "/selectByDeviceId",method = RequestMethod.GET)
    public AjaxResult selectByDeviceId(String deviceId) {
        List<AddDeviceVo> device = deviceService.selectByDeviceId(deviceId);
        if(device.size()<1) {
            return AjaxResult.success("无");
        }else {
            return AjaxResult.success(device.get(0));
        }
    }
}
